package com.lanou.sfu.songforu.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.VideoActivity;
import com.lanou.sfu.songforu.adapter.SearchMVAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.datas.SearchMV;
import com.lanou.sfu.songforu.singleton.VolleySingleton;
import com.lanou.sfu.songforu.utils.utils.NoScrollListView;

import java.util.List;

/**
 * Created by quyan on 15/10/28.
 */
public class SearchingMVFragment extends BaseFragment implements AdapterView.OnItemClickListener {

    private TextView mvSongNum;
    private RelativeLayout mvLayout;
    private NoScrollListView mvList;
    private String mvUrl;
    private SearchMVAdapter adapter;
    private VolleySingleton volleySingleton;
    private List<SearchMV.DataEntity> data;

    public static SearchingMVFragment passData(Context mContext, String str) {
        SearchingMVFragment fragment = new SearchingMVFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", str);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.search_mv_layout, null);
        mvSongNum = (TextView) view.findViewById(R.id.mv_song_num);
        mvLayout = (RelativeLayout) view.findViewById(R.id.mv_layout);
        mvList = (NoScrollListView) view.findViewById(R.id.mv_list);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        Bundle bundle = getArguments();
        String url = bundle.getString("url");
        mvUrl = Url.SEARCH_MV + url + Url.SEARCH_MV_LAST;
        getInternetData();
        mvList.setOnItemClickListener(this);
    }

    //从网络上拉取数据
    private void getInternetData() {
        StringRequest request = new StringRequest(mvUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Gson gson = new Gson();
                SearchMV entity = gson.fromJson(s, SearchMV.class);
                if (entity != null) {
                    data = entity.getData();
                    adapter = new SearchMVAdapter(getActivity(),data);
                    mvList.setAdapter(adapter);
                    mvSongNum.setText(data.size() + "");
                    mvLayout.setVisibility(View.VISIBLE);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        volleySingleton.addQueue(request);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SearchMV.DataEntity str = (SearchMV.DataEntity) parent.getItemAtPosition(position);
        String url = str.getMvList().get(0).getUrl();
        Intent intent = new Intent(getActivity(), VideoActivity.class);
        intent.putExtra("url", url);
        startActivity(intent);


    }
}
